<?php
/**
 * 书籍章节
 * User: Richard
 * Date: 2020/3/14
 * Time: 16:05
 */

namespace Demo\Controller;


use Common\Controller\DemoBaseController;

class BookChapterController extends DemoBaseController
{
    private $bookChapterModel;
    private $bookModel;


    function __construct()
    {
        parent::__construct();
        $this->bookChapterModel = M('book_chapters');
        $this->bookModel = M('books');
    }

    /**
     * 书籍章节列表
     */
    public function index()
    {
        $pageCount = I('count', 20, 'intval');//一页20个

        $where = ['status' => 1];
        $name = I('name');
        if (!empty($name)) {
            $where['name|desc'] = ['like', '%' . $name . '%'];
        }

        $count = $this->bookChapterModel->where($where)->count();
        $page = $this->page($count, $pageCount);

        $list = $this->bookChapterModel->where($where)->limit($page->firstRow, $page->listRows)->select();
        $this->returnok(['list' => $list, 'page' => $page]);
    }

    /**
     * 添加书籍章节提交
     */
    public function add_post()
    {
        $bookId = I('book_id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效的书籍');
        }

        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写书籍章节名称');
        }
        $content = I('content');
        if (empty($content)) {
            $this->returnerror('', '请填写内容');
        }

        $audio = I('audio');

        $data = [
            'name' => $name,//书籍章节名称
            'content' => $content,//封面图片
            'audio' => $audio,//音频
        ];
        $id = $this->bookChapterModel->add($data);
        if ($id) {
            $this->returnok(['id' => $id], '添加成功');
        } else {
            $this->returnerror('', '添加失败，请重试');
        }
    }

    /**
     * 编辑书籍章节
     */
    public function edit()
    {
        $bookId = I('book_id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效请求');
        }
        $bookInfo = $this->bookModel->where(['id' => $bookId, 'status' => 1])->find();
        if (empty($bookInfo)) {
            $this->returnerror('', '未找到书籍或书籍已被管理员删除');
        }

        $id = I('id', 0, 'intval');
        if (empty($id)) {
            $this->returnerror('', '无效的章节');
        }
        $chapters = $this->bookChapterModel->where(['id' => $id, 'status' => 1])->find();
        if (empty($chapters)) {
            $this->returnerror('', '未找到书籍章节或书籍章节已被管理员删除');
        }

        $this->returnok(['chapters' => $chapters]);
    }

    /**
     * edit书籍章节提交
     */
    public function edit_post()
    {
        $bookId = I('book_id', 0, 'intval');
        if (empty($bookId)) {
            $this->returnerror('', '无效请求');
        }
        $bookInfo = $this->bookModel->where(['id' => $bookId, 'status' => 1])->find();
        if (empty($bookInfo)) {
            $this->returnerror('', '未找到书籍或书籍已被管理员删除');
        }

        $id = I('id', 0, 'intval');
        if (empty($id)) {
            $this->returnerror('', '无效的章节');
        }
        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写书籍章节名称');
        }
        $content = I('content');
        if (empty($content)) {
            $this->returnerror('', '请填写内容');
        }

        $audio = I('audio');

        $data = [
            'name' => $name,//书籍章节名称
            'content' => $content,//封面图片
            'audio' => $audio,//音频
        ];
        $res = $this->bookChapterModel->where(['id' => $bookId])->save($data);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $id], '修改成功');
        } else {
            $this->returnerror('', '修改失败，请重试');
        }
    }

    /**
     * 书籍章节软删除
     */
    public function softDelete()
    {
        $chapterId = I('id', 0, 'intval');
        if (empty($chapterId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->bookChapterModel->where(['id' => $chapterId])->save(['status' => 0]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $chapterId], '删除成功');
        } else {
            $this->returnerror('', '删除失败，请重试');
        }
    }

    /**
     * 书籍章节软删除恢复
     */
    public function reply()
    {
        $chapterId = I('id', 0, 'intval');
        if (empty($chapterId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->bookChapterModel->where(['id' => $chapterId])->save(['status' => 0]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $chapterId], '恢复成功');
        } else {
            $this->returnerror('', '恢复失  败，请重试');
        }
    }


}